package com.dq.ad;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.qq.e.o.HXADConfig;
import com.qq.e.o.ads.v2.HXSdk;
import com.qq.e.o.ads.v2.ads.banner.BannerAD;
import com.qq.e.o.ads.v2.ads.banner.BannerADListener;
import com.qq.e.o.ads.v2.ads.interstitial.InterstitialAD;
import com.qq.e.o.ads.v2.ads.interstitial.InterstitialADListener;
import com.qq.e.o.ads.v2.ads.nativ.NativeAD;
import com.qq.e.o.ads.v2.ads.nativ.NativeADListener;
import com.qq.e.o.ads.v2.ads.splash.SplashAD;
import com.qq.e.o.ads.v2.ads.video.FullscreenVideoAD;
import com.qq.e.o.ads.v2.ads.video.FullscreenVideoADListener;
import com.qq.e.o.ads.v2.ads.video.RewardVideoAD;
import com.qq.e.o.ads.v2.ads.video.RewardVideoADListener;
import com.qq.e.o.ads.v2.error.AdError;
import com.qq.e.o.utils.DisplayUtil;

public class AdManager {

    private static final String TAG = "AdManager";
    private static AdManager INSTANCE;

    private BannerAD bad;
    private NativeAD nad;
    private InterstitialAD iad;
    private FullscreenVideoAD fvAd;
    private RewardVideoAD rvAd;
    private OnFullscreenVideoADListener fvAdListener;
    private OnRewardVideoADListener rvAdListener;

    public boolean isAdOpen = false;

    public static int NATIVE_AD_TYPE_ACROSS_IMAGE = HXADConfig.NATIVE_AD_TYPE.ACROSS_IMAGE;
    public static int NATIVE_AD_TYPE_VERTICAL_IMAGE = HXADConfig.NATIVE_AD_TYPE.VERTICAL_IMAGE;

    public float getScreenWidthDp(Context context) {
        return DisplayUtil.getScreenWidthDp(context);
    }

    public static AdManager getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new AdManager();
        }
        return INSTANCE;
    }

    public void useTextureView(boolean use) {
        HXADConfig.useTextureView(use);
    }

    public void supportMultiProcess(boolean support) {
        HXADConfig.supportMultiProcess(support);
    }

    public void initSDK(Context context, String chId, String cpId, boolean isDebug, boolean isLog) {
        initSDK(context, chId, cpId, isDebug, isLog, true);
    }

    /**
     * 竖屏广告开关
     *
     * @return
     */

    public boolean aDVerticalImage() {
//        if (HXADConfig.aicsMap != null && HXADConfig.aicsMap.size() > 0) {
//            for (int i = 0; i < HXADConfig.aicsMap.size(); i++) {
//                aics aics_i = HXADConfig.aicsMap.get(i);
//                int apt = aics_i.getApt();
//                if (HXADConfig.AD_TYPE.NATIVE == apt) {
//                    int iat = aics_i.getIat();
//                    if (iat == AdManager.NATIVE_AD_TYPE_VERTICAL_IMAGE) {
//                        return true;
//                    }
//                }
//            }
//        }

        if (HXADConfig.aicsMap != null && HXADConfig.aicsMap.get(44) != null) {
            return true;
        }

        return false;
    }

    /**
     * 初始化广告SDK
     *
     * @param context context
     * @param chId    渠道号
     * @param cpId    CP号
     * @param isDebug 是否测试服广告 默认值false
     * @param isLog   是否打印log 建议BuildConfig.DEBUG
     * @param isCrash 是否拦截崩溃异常 默认值true
     */
    public void initSDK(Context context, String chId, String cpId, boolean isDebug, boolean isLog, boolean isCrash) {
        HXADConfig.setDEBUG(isDebug);
        HXADConfig.setLogger(isLog);
        HXSdk.initSDK(context, chId, cpId, isCrash);
    }

    /**
     * 显示原生广告
     */
    public void showNativeAD() {
        if (nad.isPreloaded())
            nad.showAD();
        else
            nad.loadAD();
    }

    public void showFullscreenVideoAD(Activity activity) {
        showFullscreenVideoAD(activity, null);
    }

    /**
     * 显示全屏视频广告
     */
    public void showFullscreenVideoAD(Activity activity, OnFullscreenVideoADListener listener) {
        if (fvAd.isPreloaded()) {
            if (listener != null) {
                fvAdListener = listener;
            }
            fvAd.showAD(activity);
        } else {
            loadFullscreenVideoAD(activity, false, listener);
        }
    }

    public void showRewardVideoAD(Activity activity) {
        showRewardVideoAD(activity, null);
    }

    /**
     * 显示激励视频广告
     */
    public void showRewardVideoAD(Activity activity, OnRewardVideoADListener listener) {
        if (rvAd.isPreloaded()) {
            if (listener != null) {
                rvAdListener = listener;
            }
            rvAd.showAD(activity);
        } else {
            loadRewardVideoAD(activity, false, listener);
        }
    }

    /**
     * 启动界面进行预加载
     *
     * @param activity
     */
    public void preLoadSplash(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                isAdOpen = HXADConfig.isAdOpen();
                loadFullscreenVideoAD(activity, true);
                loadRewardVideoAD(activity, true);
            }
        });
    }

    /**
     * 加载开屏广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adListener  监听回调
     */
    public void showSplashAD(Activity activity, ViewGroup adContainer, final SplashADListener adListener) {
        new SplashAD(activity, adContainer, new com.qq.e.o.ads.v2.ads.splash.SplashADListener() {
            @Override
            public void onNoAD(AdError adError) {
                adListener.onNoAD(adError);
            }

            @Override
            public void onADPresent() {
                adListener.onADPresent();
            }

            @Override
            public void onADClicked() {
                adListener.onADClicked();
            }

            @Override
            public void onADSkip() {
                adListener.onADSkip();
            }

            @Override
            public void onADTimeOver() {
                adListener.onADTimeOver();
            }
        });
    }

    /**
     * 加载插屏广告
     *
     * @param activity activity
     */
    public void showInterstitialAD(final Activity activity) {
        getInterstitialAD(activity, new InterstitialADListener() {
            @Override
            public void onADReceive() {
                Log.i(TAG, "InterstitialAD onADReceive");
            }

            @Override
            public void onADExposure() {
                Log.i(TAG, "InterstitialAD onADExposure");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "InterstitialAD onADClicked");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "InterstitialAD onADClose");
            }

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "InterstitialAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }
        });
        iad.loadAD();
    }

    private void getInterstitialAD(Activity activity, InterstitialADListener adListener) {
        if (iad != null) {
            iad.closeAD();
            iad.destroy();
            iad = null;
        }
        iad = new InterstitialAD(activity, adListener);
    }

    /**
     * 加载原生信息流广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adWidth     广告宽度（dp）
     * @param adHeight    广告高度（dp）
     * @param adStyle     广告样式（参照HXInfoAdStyle类）
     */
    public void showNativeAD(final Activity activity, final ViewGroup adContainer, final int adWidth, final int adHeight, final int adStyle, final boolean isPreload) {
        nad = new NativeAD(activity, adContainer, adWidth, adHeight, adStyle, new NativeADListener() {
            @Override
            public void onSuccess(int i, View view) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "NativeAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }

            @Override
            public void onADPresent() {
                Log.i(TAG, "NativeAD onADPresent");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "NativeAD onADClicked");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "NativeAD onADClosed");
                if (isPreload) {
                    showNativeAD(activity, adContainer, adWidth, adHeight, adStyle, true);
                }
            }

            @Override
            public void onPreload() {
                Log.i(TAG, "NativeAD onPreload");
            }
        });
        if (isPreload) {
            nad.preloadAD();
        } else {
            nad.loadAD();
        }
    }

    /**
     * 加载横幅广告
     *
     * @param activity    activity
     * @param adContainer 容器
     * @param adHeight    广告高度（dp）
     */
    public void showBannerAD(Activity activity, ViewGroup adContainer, int adHeight) {
        bad = new BannerAD(activity, adContainer, adHeight, new BannerADListener() {

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "BannerAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }

            @Override
            public void onADPresent() {
                Log.i(TAG, "BannerAD onADPresent");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "BannerAD onADClicked");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "BannerAD onADClosed");
                closeBannerAD();
            }
        });
        bad.loadAD();
    }

    /**
     * 关闭横幅广告
     */
    public void closeBannerAD() {
        if (bad != null) {
            bad.destroy();
            bad = null;
        }
    }

    public void loadFullscreenVideoAD(final Activity activity, final boolean isPreload) {
        loadFullscreenVideoAD(activity, isPreload, null);
    }

    /**
     * 预加载或加载全屏视频广告
     *
     * @param activity  activity
     * @param isPreload 是否预加载
     */
    public void loadFullscreenVideoAD(final Activity activity, final boolean isPreload, final OnFullscreenVideoADListener adListener) {
        getFullscreenVideoAD(activity, new FullscreenVideoADListener() {

            @Override
            public void onADLoad() {
                Log.i(TAG, "FullscreenVideoAD onADLoad");
            }

            @Override
            public void onVideoCached() {
                Log.i(TAG, "FullscreenVideoAD onVideoCached");
            }

            @Override
            public void onADShow() {
                Log.i(TAG, "FullscreenVideoAD onADShow");
            }

            @Override
            public void onADExpose() {
                Log.i(TAG, "FullscreenVideoAD onADExpose");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "FullscreenVideoAD onADClicked");
            }

            @Override
            public void onVideoComplete() {
                Log.i(TAG, "FullscreenVideoAD onVideoComplete");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "FullscreenVideoAD onADClosed");
                if (adListener != null) {
                    adListener.onClosed();
                }
                if (fvAdListener != null) {
                    fvAdListener.onClosed();
                }
                if (isPreload) {
                    loadFullscreenVideoAD(activity, true);
                }
            }

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "FullscreenVideoAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
                if (adListener != null) {
                    adListener.onFailed(i, adError);
                }
                if (fvAdListener != null) {
                    fvAdListener.onFailed(i, adError);
                }
            }

            @Override
            public void onSkippedVideo() {
                Log.i(TAG, "FullscreenVideoAD onSkippedVideo");
            }

            @Override
            public void onPreload() {
                Log.i(TAG, "FullscreenVideoAD onPreload");
            }
        });
        if (isPreload) {
            fvAd.preloadAD();
        } else {
            fvAd.loadAD();
        }
    }

    private void getFullscreenVideoAD(Activity activity, FullscreenVideoADListener adListener) {
        fvAd = new FullscreenVideoAD(activity, adListener);
    }

    public void loadRewardVideoAD(final Activity activity, final boolean isPreload) {
        loadRewardVideoAD(activity, isPreload, null);
    }

    /**
     * 预加载或加载激励视频广告
     *
     * @param activity  activity
     * @param isPreload 是否预加载
     */
    public void loadRewardVideoAD(final Activity activity, final boolean isPreload, final OnRewardVideoADListener adListener) {
        getRewardVideoAD(activity, new RewardVideoADListener() {

            @Override
            public void onADLoad() {
                Log.i(TAG, "RewardVideoAD onADLoad");
            }

            @Override
            public void onVideoCached() {
                Log.i(TAG, "RewardVideoAD onVideoCached");
            }

            @Override
            public void onADShow() {
                Log.i(TAG, "RewardVideoAD onADShow");
            }

            @Override
            public void onADExpose() {
                Log.i(TAG, "RewardVideoAD onADExpose");
            }

            @Override
            public void onReward() {
                Log.i(TAG, "RewardVideoAD onReward");
                if (adListener != null) {
                    adListener.onReward();
                }
                if (rvAdListener != null) {
                    rvAdListener.onReward();
                }
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "RewardVideoAD onADClicked");
            }

            @Override
            public void onVideoComplete() {
                Log.i(TAG, "RewardVideoAD onVideoComplete");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "RewardVideoAD onADClosed");
                if (adListener != null) {
                    adListener.onClosed();
                }
                if (rvAdListener != null) {
                    rvAdListener.onClosed();
                }
                if (isPreload) {
                    loadRewardVideoAD(activity, true);
                }
            }

            @Override
            public void onSuccess(int i) {

            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "RewardVideoAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
                if (adListener != null) {
                    adListener.onFailed(i, adError);
                }
                if (rvAdListener != null) {
                    rvAdListener.onFailed(i, adError);
                }
            }

            @Override
            public void onSkippedVideo() {
                Log.i(TAG, "RewardVideoAD onSkippedVideo");
            }

            @Override
            public void onPreload() {
                Log.i(TAG, "RewardVideoAD onPreload");
            }
        });
        if (isPreload) {
            rvAd.preloadAD();
        } else {
            rvAd.loadAD();
        }
    }

    private void getRewardVideoAD(Activity activity, RewardVideoADListener adListener) {
        rvAd = new RewardVideoAD(activity, adListener);
    }

    //  加载广告
    public void loadNativeAd(final Activity a, int widthDp, int heightDp, int adType, final InterfaceNativeADLoad lis) {
        new NativeAD(a, null, widthDp, heightDp, adType, new NativeADListener() {
            @Override
            public void onSuccess(int i, View view) {
                lis.success(view);
            }

            @Override
            public void onFailed(int i, AdError adError) {
                Log.i(TAG, "NativeAD onFailed, eCode=" + adError.getErrorCode() + " msg:" + adError.getErrorMsg());
            }

            @Override
            public void onADPresent() {
                Log.i(TAG, "NativeAD onADPresent");
            }

            @Override
            public void onADClicked() {
                Log.i(TAG, "NativeAD onADClicked");
            }

            @Override
            public void onADClosed() {
                Log.i(TAG, "NativeAD onADClosed");
            }

            @Override
            public void onPreload() {
                Log.i(TAG, "NativeAD onPreload");
            }
        }, false).loadAD();
    }

    public interface InterfaceNativeADLoad {
        public void success(View v);
    }

    public interface OnFullscreenVideoADListener {
        void onClosed();

        void onFailed(int i, AdError adError);
    }

    public interface SplashADListener {
        void onNoAD(Object var1);

        void onADPresent();

        void onADClicked();

        void onADSkip();

        void onADTimeOver();
    }

    public interface OnRewardVideoADListener {
        void onClosed();

        void onReward();

        void onFailed(int i, Object adError);
    }
}